package com.elvt.server.controller;

import com.elvt.server.pojo.Admin;
import com.elvt.server.service.IAdminService;
import com.elvt.server.utils.RespSult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author 作者： elvt
 * @Data 时间：2022/6/5
 * @packageName 包路径： ybg_back_end
 * @Desciption 说明： 个人中心
 */
@RestController
public class AdminInfoCotroller {
    @Autowired
    private IAdminService adminService;

     @ApiOperation("更新当前用户信息")
    @PutMapping(value = "/admin/info")
    public RespSult updateAdmin(@RequestBody Admin admin, Authentication authentication){
         if (adminService.updateById(admin)){
             SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
             return RespSult.success("更新成功！");
         }
            return RespSult.error("更新失败！");
     }
     @ApiOperation(value = "更新用户密码")
     @PutMapping("/admin/pass")
     public RespSult updateAdminPassword(@RequestBody Map<String, Object> info){
         String oldPass = (String) info.get("oldPass");
         String pass = (String) info.get("pass");
         Integer adminId = (Integer) info.get("adminId");
         return adminService.updateAdminPassword(oldPass,pass,adminId);
     }

}
